function resetImg(path, callback) {
	var img = new Image();
	img.onload = function() {
		EXIF.getData(img, function() {
			var Orientation = EXIF.getTag(this, 'Orientation');
			setTimeout(function() {
				if(Orientation != "" && Orientation != 1 && Orientation) {
					switch(Orientation) {
						case 6: //需要顺时针（向左）90度旋转  
							rotateImgNew(img, 'left', callback);
							break;
						case 8: //需要逆时针（向右）90度旋转  
							rotateImgNew(img, 'right', callback);
							break;
						case 3: //需要180度旋转  
							var _this = img;
							rotateImgNew(img, 'right', function() {
								rotateImgNew(_this, 'right', callback);
							}); //转两次 
							break;
					}
				} else {
					callback(path)
				}
			}, 1000);
		});
	}
	img.src = path;
}

function rotateImgNew(img, direction, callback) {
	var canvas = document.createElement("canvas");
	//最小与最大旋转方向，图片旋转4次后回到原方向    
	var min_step = 0;
	var max_step = 3;
	if(img == null) return;
	//img的高度和宽度不能在img元素隐藏后获取，否则会出错    
	var height = img.height;
	var width = img.width;
	var step = 2;
	if(step == null) {
		step = min_step;
	}
	if(direction == 'right') {
		step++;
		//旋转到原位置，即超过最大值    
		step > max_step && (step = min_step);
	} else {
		step--;
		step < min_step && (step = max_step);
	}
	//旋转角度以弧度值为参数    
	var degree = step * 90 * Math.PI / 180;
	var ctx = canvas.getContext('2d');
	switch(step) {
		case 0:
			canvas.width = width;
			canvas.height = height;
			ctx.drawImage(img, 0, 0);
			break;
		case 1:
			canvas.width = height;
			canvas.height = width;
			ctx.rotate(degree);
			ctx.drawImage(img, 0, -height);
			break;
		case 2:
			canvas.width = width;
			canvas.height = height;
			ctx.rotate(degree);
			ctx.drawImage(img, -width, -height);
			break;
		case 3:
			canvas.width = height;
			canvas.height = width;
			ctx.rotate(degree);
			ctx.drawImage(img, -width, 0);
			break;
	}
	dat = canvas.toDataURL("image/jpeg", 0.1);
	callback(dat)
};

function comImage(tdomImg, fn) {
	var fr = new FileReader();
	fr.onload = function(e) {
		var timgdata = e.target.result;
		resetImg(timgdata, function(domImg) {
			var img = new Image();
			img.onload = function() {
				var w0 = img.width;
				var h0 = img.height;
				var maxW = 1200;
				var maxH = 1000;
				if(maxW < w0 && maxH < h0) {
					var bl = (maxW / w0) < (maxH / h0) ? maxW / w0 : maxH / h0;
					var tcan = document.createElement('canvas');
					tcan.width = w0 * bl;
					tcan.height = h0 * bl;
					tctx = tcan.getContext('2d');
					tctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, tcan.width, tcan.height);
					fn(tcan.toDataURL("image/jpeg"))
				} else {
					fn(timgdata)
				}
			}
			img.src = domImg;
		});
	}
	fr.readAsDataURL(tdomImg);
}
function convertImgDataToBlob(base64Data) {
	var format = "image/jpeg";
	var base64 = base64Data;
	var code = window.atob(base64.split(",")[1]);
	var aBuffer = new window.ArrayBuffer(code.length);
	var uBuffer = new window.Uint8Array(aBuffer);
	for(var i = 0; i < code.length; i++) {
		uBuffer[i] = code.charCodeAt(i) & 0xff;
	}
	var blob = null;
	try {
		blob = new Blob([uBuffer], {
			type: format
		});
	} catch(e) {
		window.BlobBuilder = window.BlobBuilder ||
			window.WebKitBlobBuilder ||
			window.MozBlobBuilder ||
			window.MSBlobBuilder;
		if(e.name == 'TypeError' && window.BlobBuilder) {
			var bb = new window.BlobBuilder();
			bb.append(uBuffer.buffer);
			blob = bb.getBlob("image/jpeg");

		} else if(e.name == "InvalidStateError") {
			blob = new Blob([aBuffer], {
				type: format
			});
		} else {

		}
	}
	return blob;
};
function readFile(e) {
	var readerFiles = e.target.files[0];
	comImage(readerFiles, function(dat) {
		console.log(dat);
		
//		var img = convertImgDataToBlob(dat);
//		var formData = new FormData();
//		formData.append("image", img, "file_" + Date.parse(new Date()) + ".png");
		
	});
}